﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BitLaboratory.DbAccess
{
    public class MsSqlDatabaseProvider : IDatabaseProvider
    {
        private string _connectionString;

        public MsSqlDatabaseProvider(string connectionString)
        {
            _connectionString = connectionString;
        }

        public IDbCommand CreateDbCommand()
        {
            SqlCommand comm = new SqlCommand();
            comm.CommandTimeout = 300;
            return comm;
        }

        public IDbDataAdapter CreateDbDataAdapter()
        {
            return new SqlDataAdapter();
        }

        public IDbDataParameter CreateDbParameter()
        {
            IDbDataParameter parameter = new SqlParameter();
            return parameter;
        }

        public IDbDataParameter CreateDbParameter(string parameterName, object parameterValue, ParameterDirection parameterDirection = ParameterDirection.Input)
        {
            IDbDataParameter parameter = new SqlParameter(parameterName, parameterValue);
            parameter.Direction = parameterDirection;
            return parameter;
        }

        public IDbDataParameter CreateDbParameter<TValueType>(string parameterName, TValueType parameterValue, ParameterDirection parameterDirection = ParameterDirection.Input)
        {
            IDbDataParameter parameter = new SqlParameter(parameterName, parameterValue);
            parameter.Direction = parameterDirection;
            return parameter;
        }

        public IDbConnection CreateDbConnection()
        {
            SqlConnection conn = new SqlConnection(this._connectionString);
            return conn;
        }

        public IDbDataAdapter CreateDbDataAdapter(IDbCommand selectCommand)
        {
            return new SqlDataAdapter((SqlCommand)selectCommand);
        }

        public string ConnectionString 
        {
            get { return _connectionString; }
        }
    }
}
